﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMT.SagePos.DataAccess;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Linq.Expressions;
using System.Data.Linq;

namespace SMT.SagePos.BusinessObjects
{
    public static class Test
    {
        public static string Go() 
        {
            StringBuilder sb = new StringBuilder();
            
            Customers customers = new Customers();
            customers.LoadCustomers(c => c.FirstName.StartsWith("B"));

            

            foreach (Customer c in (IEnumerable<Customer>)customers)
                sb.Append(c.Account + ": " + c.FirstName + Environment.NewLine);

            return sb.ToString();
        }
    }

    //4 steps: pass in unique value to find a record with, implement GetItem, optionally implement Validate, optionally implement OnInsert
    [LinqClass("Customer")]
    public class Customer: BusinessObjectBase //, IEditableObject
    {
        
        public Customer(DataAccess.Customer customer) : base(customer) 
        {
            this.Validate += Validate_Customer;
        }

        protected void Validate_Customer()
        {
            if (FirstName == null || FirstName.Length == 0)
                throw new InvalidOperationException("First Name is a required field");            
        }

        #region Properties

        [LinqProperty("cus_id")]           public override int? id      { get; protected set; }
        [LinqProperty("cus_account")]      public int?      Account     { get; set; }
        [LinqProperty("cus_fname")]        public string    FirstName   { get; set; }
        [LinqProperty("cus_lname")]        public string    LastName    { get; set; }
        [LinqProperty("cus_isabusiness")]  public bool      IsABusiness { get; set; }
        [LinqProperty("cus_license")]      public string    License     { get; set; }
        [LinqProperty("cus_company")]      public string    Company     { get; set; }
        [LinqProperty("cus_email")]        public string    Email       { get; set; }
        [LinqProperty("cus_city")]         public string    City        { get; set; }
        [LinqProperty("cus_state")]        public string    State       { get; set; }
        [LinqProperty("cus_zip")]          public string    Zip         { get; set; }
        [LinqProperty("cus_country")]      public string    Country     { get; set; }
        [LinqProperty("cus_home")]         public string    Home        { get; set; }
        [LinqProperty("cus_work")]         public string    Work        { get; set; }
        [LinqProperty("cus_cell")]         public string    Cell        { get; set; }
        [LinqProperty("cus_taxexemptid")]  public string    TaxExemptId { get; set; }
        [LinqProperty("cus_created")]      public DateTime? Created     { get; set; }

        #endregion

        //#region IEditableObject implementation

        //public void BeginEdit()
        //{
        //    throw new NotImplementedException();
        //}

        //public void CancelEdit()
        //{
        //    throw new NotImplementedException();
        //}

        //public void EndEdit()
        //{
        //    throw new NotImplementedException();
        //}

        //#endregion
    
    }
    
 
    [LinqClass("Customers")]
    public class Customers : BusinessObjectCollectionBase<DataAccess.Customer>
    {
        public override object GetItem(dynamic item)
        {
            return new Customer(item);
        }

        public void LoadCustomers(Func<Customer, bool> condition)
        {
            this.Load(condition);  
        }

        public void LoadPage(int lowerBoundary, int numRows, string propertyToOrderBy)
        {
            base.LoadPage<Customer>(lowerBoundary, numRows, propertyToOrderBy);
        }


    }
 

//    public class Customers: IEnumerable, IListSource//: CollectionBase, IBindingList
//    {
//        protected POSDataContext pdc = new POSDataContext();

//        public Customers()
//        {
//            //pdc.Customers.Where                
//        }



//        #region IList implementation

////        public int Add(Customer customer) { return List.Add(customer); }

//        //public void Remove(int index)
//        //{
//        //    if (index >= Count || index < 0)
//        //        throw new InvalidOperationException("Index not valid");
//        //    else
//        //        List.RemoveAt(index);
//        //}

//        //public Customer AddNew()
//        //{
//        //    return (Customer)((IBindingList)this).AddNew();
//        //}

//        //public IEnumerable<char> this[int index]
//        //{
            
//        //    get 
//        //    {
       
//        //    }
//            //set 
//            //{ 
//            //    List[index] = value; 
//            //}
//        //}

//        #endregion

//        #region IBindingList implementation

//        private ListChangedEventHandler onListChanged;

//        //object IBindingList.AddNew()
//        //{
//        //    Customer c = new Customer(new DataAccess.Customer());
//        //    List.Add(c);
//        //    return c;
//        //}

//        public event ListChangedEventHandler ListChanged
//        {
//            add { onListChanged += value; }
//            remove { onListChanged -= value; }
//        }

//        protected virtual void OnListChanged(ListChangedEventArgs ev)
//        {
//            if (onListChanged != null) onListChanged(this, ev);
//        }

//        //bool IBindingList.AllowEdit { get { return true; } }
//        //bool IBindingList.AllowNew { get { return true; } }
//        //bool IBindingList.AllowRemove { get { return true; } }
//        //bool IBindingList.SupportsChangeNotification { get { return false; } }
//        //bool IBindingList.SupportsSearching { get { return false; } }
//        //bool IBindingList.SupportsSorting { get { return false; } }
        
//        #region NotSupported
//        //void IBindingList.AddIndex(PropertyDescriptor property) { throw new NotSupportedException(); }
//        //void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction) { throw new NotSupportedException(); }
//        //int IBindingList.Find(PropertyDescriptor property, object key) { throw new NotSupportedException(); }
//        //bool IBindingList.IsSorted { get { throw new NotSupportedException(); } }
//        //void IBindingList.RemoveIndex(PropertyDescriptor property) { throw new NotSupportedException(); }
//        //void IBindingList.RemoveSort() { throw new NotSupportedException(); }
//        //ListSortDirection IBindingList.SortDirection { get { throw new NotSupportedException(); } }
//        //PropertyDescriptor IBindingList.SortProperty { get { throw new NotSupportedException(); } }
//        #endregion
//        #endregion




//        public IEnumerator GetEnumerator()
//        {
//            foreach (DataAccess.Customer c in pdc.Customers)
//                yield return new Customer(c);
//        }

//        public bool ContainsListCollection
//        {
//            get { return false; }
//        }

        

//        public IList GetList()
//        {
//            List<Customer> l = new List<Customer>();
//            foreach (Customer c in this) 
//                l.Add(c);
//            return l;
//        }
//    }
}
